package com.example.register1.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.register1.Dto.PageDTO;
import com.example.register1.domin.Register;
import com.example.register1.vo.BooksVO;
import com.example.register1.vo.NamesVO;
import com.example.register1.vo.StudentVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface RegisterMapper extends BaseMapper<Register> {
    List<StudentVO> pageQuery(PageDTO pageDTO);

    Integer selectBeginChapter(String name, LocalDate startTime);

    Integer selectEndChapter(String name, LocalDate startTime,LocalDate endTime);

    LocalDateTime selectMaxDate(String name);

    String selectNowChapter(LocalDateTime dateTime,String name);

    @Select("select MIN(start_date) from register")
    LocalDateTime selectMinDate();

    @Select("select MAX(start_date) from register")
    LocalDateTime selectmaxDate();

//    @Select("select name,chapter,content from tb_chapter")
//    List<BooksVO> selectAllBooks();

    @Select("select id_class,student_class from tb_class")
    List<NamesVO> selectAllClasses();

    @Select("select name from load11 where id_class = #{idClass} and student_class = #{studentClass}")
    String[] selectAllNames(String idClass, String studentClass);

    @Select("select name,chapter from tb_books")
    List<BooksVO> selectAllChapters();

    @Select("select content from tb_chapter where name=#{name} and chapter=#{chapter}")
    String[] selectAllContent(String name, String chapter);

    String selectNowContent(LocalDateTime dateTime, String name);

    String selectNowDay(LocalDateTime dateTime, String name);

    @Select("select id_chapter from tb_chapter where content = #{idChapter} and name=#{book} and chapter = #{chapter}")
    Integer selectByContent(String idChapter,String book,String chapter);

    @Select("select * from register where name = #{name} and id_chapter = #{idChapter} and is_end = 0")
    Register selectStu(String name, Integer idChapter);

    @Update("update register set end_date = #{endDate},time = #{time},id_chapter = #{idChapter},is_end=1 where name=#{name} and is_end = 0")
    void updateStu(Register register);

    @Update("update register set id_chapter = #{id} where name=#{name} and is_end = 0")
    void setIdChapter(String name, Integer id);

    @Select("select min(id_chapter) from register where name = #{name} and start_date between #{startTime} and #{endTime} and is_end = 1")
    Integer selectMinChapter(String name, LocalDate startTime, LocalDate endTime);

    @Select("select name from tb_chapter where id_chapter = #{c}")
    String selectChapterName(Integer c);

    @Select("select min(id_chapter) from tb_chapter where name = #{name}")
    Integer selectClosedChapter(String name);

    @Select("select min(start_date) from register where name =#{name} and is_end=1")
    LocalDateTime selectFirstDate(String name);

    @Select("select id_chapter from register where name = #{name} and is_end =1 and start_date = #{firstTime}")
    Integer selectIdChapter(String name, LocalDateTime firstTime);

//    @Update("update register set id_chapter = #{id} where name = #{name} and ")
//    void setIdChapter(Integer id);
}
